<?php

namespace app\teacher\controller\v1;

use app\common\controller\BaseController;
use app\teacher\model\Notice as NoticeModel;

/**
 * 公告
 */

class Notice extends BaseController
{
    /**
     * 首页最新公告
     */
    public function new()
    {
        $notice = NoticeModel::getLast();
        $this->success('', compact('notice'));
    }

    /**
     * 公告列表
     */
    public function index()
    {
        $size = $this->request->get('size');
        $notices = NoticeModel::getAll($size);
        $notices->total() ? $this->success('', [
            'notices' => $notices->items(),
            'total' => $notices->total()
        ]) : $this->notFoundData();
    }

    /**
     * 公告详情
     */
    public function read()
    {
        $noticeId = $this->notEmpty('notice_id');
        $notice = NoticeModel::find($noticeId);
        if (!$notice) {
            $this->errorMsg(18000);
        }
        //记录浏览量
        $res = $notice->setInc('reading');
        $res ? $this->success('', compact('notice')) : $this->forbidden();
    }

}
